package com.hncboy.chatgpt.front.voice;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class Tests {


    private static String uri = "https://southeastasia.api.speech.microsoft.com/accfreetrial/texttospeech/acc/v3.0-beta1/vcg/speak";

    public static void main(String[] args) throws IOException {

        HashMap<String, String> headers = new HashMap<>();
        headers.put("authority", "southeastasia.api.speech.microsoft.com");
        headers.put("accept", "*/*");
        headers.put("accept-language","zh-CN,zh;q=0.9" );
        headers.put("customvoiceconnectionid", "d8a3a480-dd87-11ed-8758-97b5a7fbfaf6");
        headers.put("origin", "https://speech.microsoft.com");
        headers.put("sec-ch-ua", "\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"");
        headers.put("sec-ch-mobile", "?0");
        headers.put("sec-ch-platform", "\"Windows\"");
        headers.put("sec-fetch-dest", "empty");
        headers.put("sec-fetch-mode", "cors");
        headers.put("sec-fetch-site", "same-site");
        headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36");
        headers.put("content-type", "application/json");
        headers.put("response-type", "arraybuffer");
        String ssml = """
                 <speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US">
                        <voice name="zh-CN-XiaoxiaoNeural">
                            <mstts:express-as>
                                <prosody rate="0%" pitch="0%">
                                柴迪，别睡觉了。赶紧起来干活了
                                </prosody>
                            </mstts:express-as>
                        </voice>
                    </speak>
                """;

        HashMap<String, Object> body = new HashMap<>();
        body.put("ssml",ssml);
        body.put("ttsAudioFormat","audio-24khz-160kbitrate-mono-mp3");
        body.put("offsetInPlainText",0);
        body.put("properties",Map.of("SpeakTriggerSource","AccTuningPagePlayButton"));

        HttpResponse execute = HttpRequest.post(uri).addHeaders(headers).body(JSONUtil.toJsonStr(body)).execute();
        InputStream inputStream = execute.bodyStream();
        System.out.println(inputStream);

        String fileName = "测试.mp3";
        String path = creatFile(fileName);
        writeToLocal(path +"\\"+ fileName, inputStream);
        System.out.println("*****" + path +"\\"+ fileName + "-下载成功!");
    }

    /**
     * 创建文件
     *
     * @param fileName
     * @return
     */
    public static String creatFile(String fileName) {
        File parentFile = new File("D:\\music");
        File file = new File(parentFile, fileName);
        try {
            file.createNewFile();
            System.out.println(fileName + "-创建成功,正在写入并下载......");
            return file.getParent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将InputStream写入本地文件
     *
     * @param destination 写入本地目录
     * @param input       输入流
     * @throws IOException
     */
    private static void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        input.close();
    }
}
